from services.config_service import ConfigService, ConfigKey
from services.vnpy_data_service import VnpyDataService
from dao.contract_info_dao import ContractInfoDao
from typing import List, Dict, Any, Optional, Callable
import pandas as pd
import os


class SettingsModel:
    """设置数据模型"""
    
    def __init__(self):
        self.config_service = ConfigService()
        self.contract_info_dao = ContractInfoDao()
    
    def get_workspace_path(self) -> Optional[str]:
        """获取工作区路径"""
        return self.config_service.get(ConfigKey.WORKSPACE_PATH)
    
    def set_workspace_path(self, path: str) -> None:
        """设置工作区路径"""
        self.config_service.set(ConfigKey.WORKSPACE_PATH, path)
    
    def validate_workspace_path(self, path: str) -> bool:
        """验证工作区路径是否有效（包含 .vntrader 目录）"""
        if not path:
            return False
        vntrader_path = os.path.join(path, ".vntrader")
        return os.path.isdir(vntrader_path)
    
    def get_contract_info(self) -> pd.DataFrame:
        """获取合约信息"""
        return self.contract_info_dao.get_all_contract_info()
    
    def import_contract_info_from_file(self, file_path: str) -> None:
        """从文件导入合约信息"""
        VnpyDataService.import_contract_info_from_file(file_path)
    
    def import_historical_data_from_file(self, workspace_path: str, file_path: str, 
                                       progress_callback: Callable[[int], None]) -> None:
        """从文件导入历史数据"""
        VnpyDataService.import_bar_data_from_file(workspace_path, file_path, progress_callback)